package com.example.zg3_park.CarType.add

import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.AddParkEntity
import com.example.lib_base.mvvm.BaseActivity
import com.example.zg3_park.CarType.CarTypeViewModel
import com.example.zg3_park.R
import com.example.zg3_park.databinding.ActivityAddParkBinding

import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/AddParkActivity")
class AddParkActivity : BaseActivity<ActivityAddParkBinding,CarTypeViewModel>() {
    var area = "A区停车场"  //默认不选为这个
    var type = "标准车位"
    var areaId = 0  //区域Id
    var statu = "空闲"
    override fun intiView() {
        binding.ivBack.setOnClickListener {
            finish()
        }

        //备注 字数  输入要求
        binding.etBei.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                val length = p0?.length
                binding.tvCount.setText("${length}/10")
                if(length == 10) {
                    binding.tvCount.setTextColor(Color.RED)
                }else {
                    binding.tvCount.setTextColor(Color.DKGRAY)
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }

        })

        //添加车位
        binding.ivAdd.setOnClickListener {
            //主键Id都不给
            //车位编号
            val num = binding.etNum.text.toString()
            //备注
            val bei = binding.etBei.text.toString()
            val item = AddParkEntity(areaId,area,null,"",bei,num,statu,type,0,"")
            vm.addPark(item)
        }


        val areas = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val areaAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areas)
        areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.spArea.adapter = areaAdapter
        //下拉事件
        binding.spArea.onItemSelectedListener = object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                area = areas[p2]
                areaId = p2 + 1
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        val types = arrayOf("标准车位", "VIP车位")
        val typeAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, types)
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.spType.adapter = typeAdapter
        //下拉事件
        binding.spType.onItemSelectedListener = object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                type = types[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        binding.rgStatus.setOnCheckedChangeListener { radioGroup, i ->
            when(i) {
                R.id.rb_kongxian ->statu = "空闲"
                R.id.rb_zhanyong ->statu = "已占用"
                else -> statu = "维修中"
            }
        }
    }

    override fun intiData() {
        vm.addParkData.observe(this) {
            ToastUtils.showLong("添加成功")
            finish()
        }
    }
}